﻿using Candy.Infrastructure;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Windows;

namespace Candy.ViewModel
{
    public class InstallViewModel : ViewModelBase
    {

        public bool IsInstalled
        {
            get
            {
                return App.Current.InstallState == InstallState.Installed;
            }

        }

        INavigationService _navigationService;
        public InstallViewModel(INavigationService navigationService)
        {
            _navigationService = navigationService;
            //cut and paste this line to your ctor
            _install = new RelayCommand(ExecInstall, CanInstall);
            _stayInBrowser = new RelayCommand(ExecStayInbrowser, CanStayInbrowser);
            App.Current.InstallStateChanged += new EventHandler(Current_InstallStateChanged);
        }

        void Current_InstallStateChanged(object sender, EventArgs e)
        {
            RaisePropertyChanged("IsInstalled");
            _install.RaiseCanExecuteChanged();
        }


        private RelayCommand _install;
        public RelayCommand Install
        {
            get
            {
                return _install;
            }
        }

        /// <summary>
        /// Checks whether the Install command is executable
        /// </summary>
        private bool CanInstall()
        {
            return App.Current.InstallState != InstallState.Installed;
        }

        /// <summary>
        /// Executes the Install command 
        /// </summary>
        private void ExecInstall()
        {
            App.Current.Install();
        }


        private RelayCommand _stayInBrowser;
        public RelayCommand StayInbrowser
        {
            get
            {
                return _stayInBrowser;
            }
        }

        /// <summary>
        /// Checks whether the StayInbrowser command is executable
        /// </summary>
        private bool CanStayInbrowser()
        {
            return true;
        }

        /// <summary>
        /// Executes the StayInbrowser command 
        /// </summary>
        private void ExecStayInbrowser()
        {
            _navigationService.Navigate(NavigationTargets.Projects);
        }





    }
}
